// 云函数模板
// 部署：在 cloud-functions/login 文件夹右击选择 “上传并部署”

const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const TcbRouter = require('tcb-router')
const db = cloud.database()
const itemCollection = db.collection('student')

/**
 * 这个示例将经自动鉴权过的小程序用户 openid 返回给小程序端
 * 
 * event 参数包含小程序端调用传入的 data
 * 
 */
exports.main = async (event, context) => {
  const openid = event.userInfo.openId

  const app = new TcbRouter({
    event
  })
  app.router('search', async (ctx, next) => {
    const result = await itemCollection.where({
      _openid: openid
    }).get().then(res => {
      return res.data
    })
    ctx.body = {
      result,
      openid
    }
  })
  return app.serve()
}